use actix_files::NamedFile;
use actix_web::web::to;
use actix_web::{App, HttpRequest, HttpServer, ResponseError, Result};
use derive_more::{Display, Error};
use std::io;

async fn index(_req: HttpRequest) -> io::Result<NamedFile> {
    Ok(NamedFile::open("static/index.html")?)
}

#[derive(Debug, Display, Error)]
#[display(fmt = "my error: {}", name)]
struct MyError {
    name: &'static str,
}

/// Use default implementation for `error_response()` method
impl ResponseError for MyError {}

async fn index2() -> Result<&'static str, MyError> {
    Err(MyError { name: "test" })
}

#[actix_web::main]
async fn main() -> io::Result<()> {
    HttpServer::new(|| App::new().route("/", to(index))
        .route("/1",to(index2)))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}
